<script setup lang="ts">
import {onMounted, ref, watch} from "vue";
import {useRouter} from 'vue-router'
import {ElMessage} from "element-plus";
import {Picture, Lock, User} from "@element-plus/icons-vue";
import {getCaptcha, login as loginReq} from "@/api/system"
import type {ICaptcha, ILoginParams} from "@/types";
import {registerDynamicRoutes} from "@/router/dynamicRoutes.ts";
import {projectRsaEncrypt} from '@/utils/rsa.ts';
import {useUserInfoStore} from "@/stores/userInfo.ts";
import {setToken} from "@/utils/auth.ts";
import {useWebInfoStore} from "@/stores/webInfo.ts";
//存储token和用户信息
const userInfoStore = useUserInfoStore();
const webInfoStore = useWebInfoStore();
const router = useRouter()
const loading = ref<boolean>(false);
const captcha = ref<ICaptcha>({
  captchaImage: "",
  captchaKey: "",
});
const state = ref<ILoginParams>({
  username: 'admin',
  password: '123',
  captchaKey: "captchaKey",
  captchaCode: "123",
});
const getCode = (): void => {
  // 获取验证码逻辑
  getCaptcha().then((res) => {
    captcha.value = res.data;
    state.value.captchaKey = res.data.captchaKey;
  }).finally(() => {
  })
}
const login = async () => {
  //非空判断
  if (!state.value.username) {
    ElMessage.error('请输入用户名');
    return;
  }
  if (!state.value.password) {
    ElMessage.error('请输入密码');
    return;
  }
  if (!state.value.captchaCode) {
    ElMessage.error('请输入验证码');
    return;
  }
  loading.value = true;
  const encryptedPsw = await projectRsaEncrypt(state.value.password);
  loginReq({
    ...state.value,
    password: encryptedPsw,
  }).then(async (res) => {
      setToken(res.data.token)
      console.log(userInfoStore)
      userInfoStore.setUserInfo(res.data)
      await registerDynamicRoutes(router);
      // 登录成功
      ElMessage.success('登录成功');
      await router.push({path: '/system'});
    }
  ).catch((err) => {
    console.error(err)
  }).finally(() => {
    loading.value = false;
  })
}

const open = () => {
  window.open("https://beian.miit.gov.cn", "_blank")
}

onMounted(() => {
  document.title = "用户登录"
  getCode();
})
</script>

<template>
  <div class="wrap">
    <div class="box">
      <div class="title-name">
        <h2>{{ webInfoStore?.webInfo?.site_name}}</h2>
      </div>

      <div class="login-form" @keyup.enter.capture="login">
        <div class="welcome-text">用户登录</div>
        <div class="item">
          <el-input size="large" v-model="state.username" placeholder="用户名" :prefix-icon="User"/>
        </div>

        <div class="item">
          <el-input size="large" type="password" show-password v-model="state.password"
                    placeholder="密码"
                    :prefix-icon="Lock"/>
        </div>

        <div class="item captcha-container">
          <el-input size="large" v-model="state.captchaCode"
                    placeholder="验证码"
                    :prefix-icon="Picture"/>
          <div class="captcha-img">
            <img @click="getCode" :src="captcha.captchaImage" alt="验证码">
          </div>
        </div>

        <div class="item captcha-refresh">
          <p @click="getCode">看不清？点击更换</p>
        </div>

        <div class="item">
          <el-button @click="login" :loading="loading" class="login-button" type="primary"
                     size="large">
            登录系统
          </el-button>
        </div>

        <div class="system-info">
          <p>登录即视为你同意 <span>《用户协议》</span>、<span>《隐私政策》</span>
          </p>
        </div>
      </div>
    </div>

    <div class="geometric-shape shape1"></div>
    <div class="geometric-shape shape2"></div>
    <div class="geometric-shape shape3"></div>

    <div class="footer" @click="open">
      {{ webInfoStore?.webInfo?.copyright }}&nbsp;&nbsp;&nbsp;{{ webInfoStore?.webInfo?.icp }}
    </div>
  </div>
</template>
<style scoped>
:root {
  --primary-color: #1890ff;
  --secondary-color: #096dd9;
  --text-color: #333;
  --light-text: #fff;
  --border-radius: 8px;
  --box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.wrap {
  height: 100vh;
  background: linear-gradient(135deg, #1c92d2 0%, #0c4a6e 100%);
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.system-title h1 {
  color: white;
  font-size: 36px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  letter-spacing: 1px;
}

.box {
  width: 420px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: var(--border-radius);
  padding: 30px;
  box-shadow: var(--box-shadow);
  position: relative;
  z-index: 10;
  animation: fadeIn 0.8s ease-out;
  height: auto;
  margin: 0;
  top: auto;
  right: auto;
  bottom: auto;
  left: auto;
  overflow: visible;
}

.title-name {
  text-align: center;
  margin-bottom: 25px;
  background-color: transparent;
}

.title-name h2 {
  color: var(--text-color);
  font-size: 24px;
  font-weight: 600;
  margin-top: 10px;
  padding: 0;
  margin-bottom: 0;
  color: #333;
}

.logo-container {
  display: flex;
  justify-content: center;
  margin-bottom: 15px;
}

.login-form {
  padding: 0 10px;
}

.welcome-text {
  text-align: center;
  color: #666;
  margin-bottom: 20px;
  font-size: 16px;
}

.item {
  margin-bottom: 20px;
  margin: 0 0 20px 0;
}

.item :deep(.el-input__wrapper) {
  border-radius: var(--border-radius);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1) inset;
  transition: all 0.3s;
}

.item :deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--primary-color) inset;
}

.item :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--primary-color) inset !important;
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-container .el-input {
  flex: 1;
}

.captcha-img {
  width: 130px;
  height: 40px;
  border-radius: var(--border-radius);
  overflow: hidden;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
}

.captcha-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: pointer;
}

.captcha-refresh {
  text-align: right;
  margin-top: -10px;
  margin-bottom: 15px;
}

.captcha-refresh p {
  font-size: 12px;
  color: var(--primary-color);
  cursor: pointer;
  user-select: none;
}

.login-button {
  width: 100%;
  height: 44px;
  border-radius: var(--border-radius);
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(to right, #1890ff, #096dd9);
  border: none;
  transition: all 0.3s;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
  background: linear-gradient(to right, #40a9ff, #1890ff);
}

.system-info {
  margin-top: 25px;
  text-align: center;
  color: #666;
  font-size: 13px;
  line-height: 1.6;
}

.footer {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 20px;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  font-size: 14px;
}

.geometric-shape {
  position: absolute;
  border-radius: 50%;
  opacity: 0.1;
  background: white;
}

.shape1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: 10%;
}

.shape2 {
  width: 500px;
  height: 500px;
  bottom: -200px;
  right: 10%;
}

.shape3 {
  width: 200px;
  height: 200px;
  top: 30%;
  right: 20%;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式布局 */
@media (max-width: 768px) {
  .box {
    width: 90%;
    max-width: 420px;
    padding: 20px;
  }

  .system-title h1 {
    font-size: 28px;
  }
}
</style>
